package com.yzh.IOCDI;

import com.yzh.IOCDI.Configuration.BeanConfig;
import com.yzh.IOCDI.Configuration.UserConfiguration;
import com.yzh.IOCDI.component.UserComponent;
import com.yzh.IOCDI.controller.HelloController;
import com.yzh.IOCDI.repository.UserRepository;
import com.yzh.IOCDI.service.UserSevice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import java.beans.Introspector;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		//三种方法获取Bean
		ApplicationContext context = SpringApplication.run(Application.class, args);

		//根据bean的名称获取bean
		HelloController bean = context.getBean(HelloController.class);
		bean.hello();
		System.out.println(bean);

		//读取controller里的bean
//		根据名称+类型获取
		HelloController bean2 = (HelloController) context.getBean("helloController");
		System.out.println(bean2);

//		根据类型获取bean
		HelloController bean3 = context.getBean("helloController", HelloController.class);
		System.out.println(bean3);

		//Bean命名规则  bean在容器里是唯一的
		System.out.println(Introspector.decapitalize("HelloController"));//首字母大写的 转小写
		System.out.println(Introspector.decapitalize("HEEEloController"));//前两个都大写的保持不变

		//读取service里的bean
		UserSevice bean4 = context.getBean(UserSevice.class);
		bean4.doService();

		//读取repository里的bean
		UserRepository bean5 = context.getBean(UserRepository.class);
		bean5.doRepository();

		//读取component里的bean    @Component 是⼀个元注解
		//另外四个算component的衍生类
		UserComponent bean6 = context.getBean(UserComponent.class);
		bean6.doComponent();

		//读取configuration里的bean
		UserConfiguration bean7 = context.getBean(UserConfiguration.class);
		bean7.doConfiguration();

		//Bean方法注解
		//@Bean注解要配合类注解使用才能生效
//		UserInfo bean8 = context.getBean(UserInfo.class);
//		System.out.println(bean8.toString());

//		一个类中有多个Bean时,根据Bean名称获取对象
		UserInfo user1 = (UserInfo) context.getBean("user1");
		UserInfo user2 = (UserInfo) context.getBean("user2");
		System.out.println(user1);
		System.out.println(user2);

		//重命名
		UserInfo yzh = (UserInfo) context.getBean("yzh");
		System.out.println(yzh);

		//路径扫描, 一般情况下就扫描启动类所在目录
		//默认扫描的范围是SpringBoot启动类所在包及其⼦包
		//可以用@ComponentScan({"com.example.demo"})来手动指定扫描路径,可以多个string数组逗号隔开

		HelloController bean8 = (HelloController) context.getBean(HelloController.class);
		System.out.println(bean8);
		bean8.hello();

		UserRepository userRepository = (UserRepository) context.getBean(UserRepository.class);
		userRepository.doRepository();

	}

}
